package com.gab.controller;

import java.util.List;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gab.utils.SendEmail;


@Controller
@RequestMapping("/customer")
public class CustomerController {
	@Autowired
	SendEmail sm;
	
	@RequestMapping("/add")
	@ResponseBody//以json类型返回
	public void add(HttpServletRequest request,HttpServletResponse response) {
		String name="迪玛希";
		int num=20;
		String addr="哈萨克斯坦阿斯塔纳迪玛希家";
		String phone="13723466538";
		String formEmail="dimash_diko@163.com";
		//多个收件人用逗号，隔开
		String toEmail="1337440758@qq.com,dimash_xll@163.com";
		//使用java发送邮件5步骤
		//1.创建sesssion
		Properties prop=new Properties();
		prop.put("mail.host","smtp.163.com" );
		prop.put("mail.transport.protocol", "smtp");
		prop.put("mail.smtp.auth", true);
		Session session=Session.getInstance(prop);
		//开启session的调试模式，可以查看当前邮件发送状态
		session.setDebug(true);
		try {
			//2.通过session获取Transport对象（发送邮件的核心API）
			Transport ts=session.getTransport();
			//3.通过邮件用户名密码链接
			//邮箱名", "授权码"
			ts.connect("dimash_diko@163.com", "diko15574708702");
		//4.创建邮件
		Message msg=sm.createSimpleMail(session,name,num,addr,phone, formEmail, toEmail);
		//5.发送电子邮件
		ts.sendMessage(msg, msg.getAllRecipients());
		}catch(Exception e) {
		e.printStackTrace();
		}
	}

}
